//请求头参数
var token = "92b2d5b9-9443-4432-851d-8baa5b02aa09";
//用户id
var userName = localStorage['MU30-lOGIN-USRNAME'];
//userName="chengcheng1";

/*
 * @[Function]       menu()    菜单栏对象
 * @[String]         f_ElementId    菜单ID
 * @[Function]       this.click()    菜单项点击放法
 * @[String]         c_ElementClass    菜单项class
 * @[String]         bottomElementId   底部线条
 */
function menu(f_ElementId) {
  $(f_ElementId).css({
    "margin": "0 auto",
    "position": "relative"
  });
  this.click = function(c_ElementClass, bottomElementId, fn) {
    var $class = $(c_ElementClass);
    for (var i = 0; i < $class.length; i++) {
      //为每个菜单项添加索引值
      $class[i].index = i;
      $($class[i]).click(function() {
        for (var a = 0; a < $class.length; a++) {
          $($class[a]).css("color", "#9B9B9B");
        }
        var f = $("#__d").width() * this.index + "px";
        $(bottomElementId).css("left", f);
        $($class[this.index]).css("color", "#6181CA");

        fn(this.index);
      });
    }
  }
}


/*
 *下拉加载，下拉刷新
 */
function initUpDownRefresh(start, limit) {

  this.start;
  this.limit;
  this.init = function(callBackFn) {
    appcan.window.setBounce({
      bounceType: [0, 1], //0: 是向下拖动1: 是向上拖动
      startPullCall: function() { //开始滑动时触发回调

      },
      downEndCall: function() { //上拉超过边界或者下拉执行回调

      },
      upEndCall: function(type) { //上拉或者下拉,超出边界之后,恢复最初状态执行
        //type  0: 是向下拖动1: 是向上拖动
        setTimeout(function() {

          appcan.frame.resetBounce(type) //恢复默认弹动
          var count;
          if (!type) {

            start = 1; //初始为0,下次上拉时从1开始计算
            callBackFn(0, 10, 0);
          } else {

            //一页=sectionCount条,每次上拉时+1.每次请求时从countPage乘以sectionCount条加载
            count = start++ * limit;
            //alert(count);
            callBackFn(count, limit, 1);
          }

        }, 1000);

      },
      color: "#f0f0f0",
      imgSettings: {
        "imagePath": "res://shuaxin.png",
        "textColor": "#530606",
        "pullToReloadText": "拖动刷新",
        "releaseToReloadText": "释放刷新",
        "loadingText": "加载中，请稍等"
      }
    });
  }


}

/*
 * @ [Object]   request  请求地址和参数
 * @ [Function] callBackFn 成功或者失败的回调
 */
function AJAXRequestData(request, callBackFn) {
  var that = this;
  this.url;
  this.data;
  this.type;
  if (request) {
    console.log(request);
    this.url = request.url;
    this.data = request.data;
    this.type = request.type;
    that.callBack = callBackFn;
    $.ajax({
      url: this.url,
      type: this.type,
      dataType: "JSON",
      data: this.data,
      success: function(res) {
        console.log(res);
        that.callBack(res)

      },
      error: function(err) {
        //console.log(err);
        that.callBack(err)

      }
    });
  }

  this.getData = function(callBack) {
    console.log(this);
    $.ajax({
      url: this.url,
      type: this.type,
      dataType: "JSON",
      data: this.data,
      success: function(res) {
        //console.log(res);
        callBack(res);
      },
      error: function(err) {
        //console.log(err);
        callBack(err);
      }
    });

  }

}

//获取当前年月日
function getNowDate() {
  var d = new Date();
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  var day = d.getDate();

  if (month < 10) {
    month = "0" + Number(month);

  }
  if (day < 10) {
    day = "0" + Number(day);
  }

  var date = year + "-" + month + "-" + day
  return date;
}
//获取当前年月日时分
function getNowDateHourMinutes() {
  var d = new Date();
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  var day = d.getDate();
  var hours = d.getHours(d);
  var minutes = d.getMinutes(d);

  if (month < 10) {
    month = "0" + Number(month);

  }
  if (day < 10) {
    day = "0" + Number(day);
  }

  var date = year + "-" + month + "-" + day + " "+hours + ":" + minutes;
  return date;
}

//选择年月日
function pickerDate(id, fn) {
  var d = new Date();
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  var day = d.getDate();
  var date = "";
  uexControl.openDatePicker(year, month, day);
  uexControl.cbOpenDatePicker = function(opId, dataType, data) {

    var obj = eval('(' + data + ')');
    if (obj.month < 10) {
      obj.month = "0" + Number(obj.month);

    }
    if (obj.day < 10) {
      obj.day = "0" + Number(obj.day);
    }

    date = obj.year + "-" + obj.month + "-" + obj.day
    $(id).html(date);
    fn(date);
  }

}
//根据时间戳获取年月日
function timestampTransitionDate(timesTamp) {
  var d = new Date(timesTamp);
  var year = d.getFullYear(d);
  var month = d.getMonth(d) + 1;
  var day = d.getDate(d);
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day
  }
  var date = year + "-" + month + "-" + day;

  return date;
}
//获取时分
function timestampTransitionTimes(timesTamp) {

  var d = new Date(timesTamp);
  var hours = d.getHours(d);
  var minutes = d.getMinutes(d);
  if (hours < 10) {
    hours = "0" + hours;
  }
  if (minutes < 10) {
    minutes = "0" + minutes
  }
  var date = hours + ":" + minutes;
  return date;
}

//弹出框
function alerts(content, buttons, fn) {
  if (buttons) {
    appcan.window.alert({
      title: '提示',
      content: content,
      buttons: buttons,
      callback: function(err, data, dataType, optId) {
        fn(data);
      }
    });
  } else {
    appcan.window.alert({
      title: '提示',
      content: content,
      callback: function(err, data, dataType, optId) {

        if (fn) {
          fn(data);
        }
      },
      buttons: '确定'
    });
  }
}

//提示框
function toasts(msg, duration) {
  if (duration) {
    appcan.window.openToast({
      msg: msg,
      duration: duration,
      position: 5,
      type: 0
    });
  } else {
    appcan.window.openToast({
      msg: msg,
      duration: -1,
      position: 5,
      type: 1
    });
  }

}

//没有数据时显示
function notData(id) {
  $(id).html('<div id="no-data">暂无数据...</div>');
}

//去除有重复属性的对象
function filtrateArray(array, key) {
  var hash = {};
  array = array.reduce(function(item, next) {
    hash[next[key]] ? '' : hash[next[key]] = true && item.push(next);
    return item
  }, []);

  return array;
}

//根据对象内的某个属性进行排序
function compare(property){

  return function(a,b){
    var value1 = a[property];
    var value2 = b[property];
    return value1 - value2;
  }
}

/*  @[Function] operatingRecord 操作记录方法
 *  @[Param String] userName  操作用户名
 *  @[Param String] operation  操作
 *  @[Param String] remark  操作备注
 */
function operatingRecord(userName,operation,remark){
    //console.log(userName,operation,remark);
    if(!userName){
      return;  
    }
    if(!operation){
        operation = "未知操作"
        remark = "无";
    }
 
    $.ajax({
      url: "http://172.31.3.190:8080/mpservice/oper_addOperationRecord.action?userName="+userName+"&operation="+operation+"&remark="+remark,
      type: "GET",
      dataType: "JSON",
      success: function(res) {
        
      },
      error: function(err) {
        
      }
    });
}
var iG={};
    iG.bgm=function() {  
        var uid = localStorage['MU30-lOGIN-USRNAME'];
        var localstr = localStorage['MU30-userInfor-' + uid];
        if(localstr == undefined){
            return; 
        }
        var localstr=JSON.parse(localstr);
        /*var localstr = {
                        "name":"郭坤",
                        "mobile":"13122321123",
                        "appPolicy":"联合办公账号"
        }*/
        var name=localstr.name 

;
        var canvas = document.getElementById('watermark')
           // var cw = $('#watermark');   
        var ctx = canvas.getContext("2d");   //返回一个用于在画布上绘图的环境
        ctx.clearRect(0,0,200,100);  //绘制之前画布清除
        ctx.font="20px 黑体";  
        ctx.rotate(-20*Math.PI/180);
        ctx.fillStyle = "rgba(0,0,0,0.1)";
        ctx.fillText(uid+'-'+name, -20, 80);     
        ctx.rotate('20*Math.PI/180');  //坐标系还原
        $('#repeat-watermark').css({  
                "position": "absolute",  
                "left": "0",  
                "top": "0",  
                "height": $(document).height(),  
                "opacity": "1",  
                "width": "100%",  
            });
        var crw = document.getElementById('repeat-watermark')
        crw.width = $(document).width();
        crw.height = $(document).height();
        ctxr = crw.getContext("2d");
        ctxr.clearRect(0,0,crw.width,crw.height);  //清除整个画布 
        var pat = ctxr.createPattern(crw, "repeat");    //在指定的方向上重复指定的元素  
        ctxr.fillStyle = pat;  
        ctxr.fillRect(0, 0, crw.width, crw.height);
        document.getElementById('repeat-watermark').style.backgroundImage = 'url("' + ctx.canvas.toDataURL() + '")';  
}
